package com.cn.yang.dao.impl;

import com.cn.yang.dao.MessageDao;
import com.cn.yang.model.message.MessageModel;
import com.cn.yang.model.news.NewsModel;
import com.cn.yang.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MessageDaoImpl implements MessageDao {
    JDBC jdbc = new JDBC();
    //添加Message的信息 留言====
    @Override
    public int addMessage(MessageModel model) {
        String sql = "insert into message(name,email,content) values ('"+model.getName()+"','"+model.getEmail()+"','"+model.getContent()+"')";
        int result = jdbc.update(sql);
        jdbc.closeConn();
        return result;
    }
//删除一条数据
    @Override
    public int deleteById(Integer id) {
        String sql = "delete from message where id = "+id;
        int result = jdbc.update(sql);
        jdbc.closeConn();
        return result;
    }
//总计多少数据
    @Override
    public int countNews() {
        String sql = "select count(*) as count  from message ";
        ResultSet result = jdbc.getResultSet(sql);
        int count = 0;
        try{
            while (result.next()){
                count = result.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
//查询数据
    @Override
    public List<MessageModel> findMessage(MessageModel model) {
        String sql = "select * from message limit "+model.getPageUtil().getPageNumber()+","+model.getPageUtil().getPageSize()+"";
        ResultSet resultSet = jdbc.getResultSet(sql);
        List<MessageModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                MessageModel message = new MessageModel();
                message.setId(resultSet.getInt("id"));
                message.setName(resultSet.getString("name"));
                message.setContent(resultSet.getString("content"));
                message.setEmail(resultSet.getString("email"));
                list.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return list;
    }
//更新数据
    @Override
    public int updateMessage(MessageModel model) {
        String sql = "update message set name = '"+model.getName()+"',eamil ='"+model.getEmail()+"',content = '"+model.getContent()+"'";
        int result = jdbc.update(sql);
        jdbc.closeConn();
        return result;
    }
//查询一条数据
    @Override
    public MessageModel selectById(Integer id) {
        String sql = "select * from message where id = "+id;
        ResultSet resultSet = jdbc.getResultSet(sql);
        MessageModel model = new MessageModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setContent(resultSet.getString("content"));
                model.setEmail(resultSet.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return model;
    }
}
